package com.yunquan.ohana.ui.calendar.event.week

import android.os.Bundle
import androidx.databinding.ObservableField
import com.google.gson.Gson
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.AppManager
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.ScheduleModel
import com.yunquan.ohana.ui.calendar.event.add.AddEventActivity
import com.yunquan.ohana.ui.calendar.event.week.dialog.BottomDeleteDialog
import com.yunquan.ohana.utils.Utils

class EventWeekChildItemViewModel(viewModel: EventWeekViewModel) : ItemViewModel<EventWeekViewModel>(viewModel) {

    var data: ScheduleModel? = null
    val title = ObservableField<String>()
    val dateTime = ObservableField<String>()

    constructor(viewModel: EventWeekViewModel, data: ScheduleModel) : this(viewModel) {
        this.data = data
        title.set(data.title)
        if (data.allDay == 1) {
            dateTime.set("All day")
        } else {
            dateTime.set("${data.startTimeStr} - ${data.endTimeStr}")
        }
    }

    val onDeleteClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val deleteDialog = BottomDeleteDialog(AppManager.appManager.currentActivity()!!)
            deleteDialog.setTitle(Utils.instance.getString(R.string.delete))
            deleteDialog.setTip(Utils.instance.getString(R.string.task_delete_tip))
            deleteDialog.setOnDeleteClick { dialog ->
                viewModel.deleteSchedule(data!!)
            }
            deleteDialog.show()
        }
    })

    val onItemClick = BindingCommand<Any>(object : BindingAction{
        override fun call() {
            val bundle = Bundle()
            bundle.putString("deviceId", viewModel.mDeviceId)
            bundle.putBoolean("userType", viewModel.mUserType)
            bundle.putString("data", Gson().toJson(data))
            viewModel.startActivityResult(AddEventActivity::class.java, bundle)
        }
    })
}